package com.mew.sas.service;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mew.common.dc.mapper.GoldWinMapper;
import com.mew.common.entity.statis.GoldWin;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class GoldRankService {

    @Autowired
    private GoldWinMapper goldWinMapper;

    public void userWin(Long userId,Long bet,Long payment){
        String yyyyMMdd = DateUtil.format(new Date(), "yyyyMMdd");
        Date time = DateUtil.parse(yyyyMMdd,"yyyyMMdd");
        long win = payment - bet;

        LambdaQueryWrapper<GoldWin> wrapper = new LambdaQueryWrapper<GoldWin>()
                .eq(GoldWin::getUserId, userId)
                .eq(GoldWin::getAddTime, time);

        List<GoldWin> goldWins = goldWinMapper.selectList(wrapper);
        if (null==goldWins||goldWins.isEmpty()){
            GoldWin goldWin = new GoldWin();
            goldWin.setUserId(userId);
            goldWin.setAddTime(time);
            goldWin.setWin(win);
            goldWinMapper.insert(goldWin);
            return;
        }

        GoldWin goldWin = goldWins.get(0);

        GoldWin newWin = new GoldWin();
        newWin.setId(goldWin.getId());
        newWin.setWin(goldWin.getWin()+win);

        int i = goldWinMapper.updateById(newWin);
    }
}
